//
//  LRCommentCellHeaderView.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/26.
//  Copyright © 2015年 leilurong. All rights reserved.
//

/**
*  评论cell列表头部view
*/
import UIKit

//声明代理协议及方法
protocol LRCommentsListHeaderViewDelegate: NSObjectProtocol {
    //头部view的按钮点击 实现加载评论刷新,提示修改按钮标题的评论数量
    func commentsListHeaderViewCommentButtonDidClick(button: UIButton)
}

class LRCommentsListHeaderView: UIView {
    // MARK: - 属性
    /// 代理
    weak var delegate: LRCommentsListHeaderViewDelegate?
    ///模型数据
    var status: LRStatues? {
        didSet{
            //设置评论按钮标题
            setButton(commentButton, title: "评论", count: status!.comments_count)
        }
    }
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置背景色
        backgroundColor = UIColor(white: 0.98, alpha: 1)
        // 添加子控件
        addSubview(commentButton)
        addSubview(hintView)
        // 添加约束
        // 评论按钮
        commentButton.ff_AlignInner(type: ff_AlignType.CenterLeft, referView: self, size: nil, offset: CGPointMake(8, 0))
        // 三角形
        hintView.translatesAutoresizingMaskIntoConstraints = false
        hintView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: commentButton, size: nil)
    }
    
    
    /**
     设置按钮文字
     :param: button 要设置的按钮
     :param: title  按钮标题
     :param: count  评论数量
     */
    func setButton(button: UIButton, title: String, count: Int) {
        if count > 10000 {
            let final = CGFloat(count)/10000.0
            //设置一位小数
            var titleText = String(format: "%.1f", arguments: [final])
            //当为整数时去掉 .0
            titleText = titleText.stringByReplacingOccurrencesOfString(".0", withString: "")
            button.setTitle(title + " " + titleText, forState: UIControlState.Normal)
        }else if count > 0 {
            button.setTitle("\(title) \(count)", forState: UIControlState.Normal)
        }
    }
    //评论按钮点击
    func commentButtonClick(button: UIButton) {
        delegate?.commentsListHeaderViewCommentButtonDidClick(button)
    }
    //MARK: - 懒加载
    ///评论按钮
    private lazy var commentButton: UIButton = {
        let button = UIButton()
        button.setTitle("评论", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "commentButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        return button
    }()
    /// 三角形
    private lazy var hintView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "statusdetail_segmented_bottom_arrow"))
        return imageView
    }()
    
    

}
